
* -------------------
* Title: 2_des.do
* -------------------

* Preface: This is the SECOND of four code files to reproduce all results
* reported in Joo, Elwert, and Munk 2024 "Labor Market Consequences of 
* Grandparenthood" from source data stored on Statistics Denmark servers.
* See ReadMe for details on data access.  This do file has been minimally
* redacted by Statistics Denmark staff to remove identifying information
* in compliance with applicable law. 

* Content: this code produces all descriptive statistics reported in the paper. 



* Set up
* ---------------
version 17.0
set seed 12345
set more off
set type float
set matsize 11000

* Define macros
* ---------------
global home "E:\workdata\704121\Wontak"
global raw "E:\workdata\704121\Wontak\raw"
global data "E:\workdata\704121\Wontak\data"
global result "E:\workdata\704121\Wontak\result"

global y "g1_empd g1_empdi g1_incomel"
global d1 "d"
global dc1 "dc"
global f1 "female teenb_g1 ager1 ager2 year yearr t1 g"
global g1 "births_t g1_workexp g1_empd g1_empdi g1_incomel g1_yed g1_ced g1_psych g1_ccs g1_dhosp g1_mstatr g1_partner p_empdi p_incomeli p_psych p_ccs p_dhosp"
global g21 "g2_1_partner g2_1_workexp g2_1_empd g2_1_incomel g2_1_yed g2_1_ced g2_1_psych g2_1_ccs g2_1_dhosp"
global g2 "g2 g3 g2_female g2_partner g2_empd g2_incomel g2_yed g2_ced g2_psych g2_ccs g2_dhosp"
global h "h_incomeg hhsize hh_g2 hh_u18 h_welfare"

global v1_g1 "L1.c.births_t L1.c.g1_workexp L1.c.g1_empd L1.i.g1_empdi L1.c.g1_incomel L1.c.g1_yed L1.i.g1_ced L1.c.g1_psych L1.c.g1_ccs L1.c.g1_dhosp L1.i.g1_partner L1.i.p_empdi L1.i.p_incomeli L1.i.p_psych L1.i.p_ccs L1.i.p_dhosp"
global v2_g1 "L2.c.births_t L2.c.g1_workexp L2.c.g1_empd L2.i.g1_empdi L2.c.g1_incomel L2.c.g1_yed L2.i.g1_ced L2.c.g1_psych L2.c.g1_ccs L2.c.g1_dhosp L2.i.g1_partner L2.i.p_empdi L2.i.p_incomeli L2.i.p_psych L2.i.p_ccs L2.i.p_dhosp"
global v1_g21 "L1.i.g2_1_partner L1.c.g2_1_workexp L1.c.g2_1_empd L1.c.g2_1_incomel L1.c.g2_1_yed L1.i.g2_1_ced L1.c.g2_1_psych L1.c.g2_1_ccs L1.c.g2_1_dhosp"
global v2_g21 "L2.i.g2_1_partner L2.c.g2_1_workexp L2.c.g2_1_empd L2.c.g2_1_incomel L2.c.g2_1_yed L2.i.g2_1_ced L2.c.g2_1_psych L2.c.g2_1_ccs L2.c.g2_1_dhosp"
global v1_g2 "L1.c.g2 L1.c.g3 L1.c.g2_female L1.c.g2_partner L1.c.g2_empd L1.c.g2_incomel L1.c.g2_yed L1.c.g2_ced L1.c.g2_psych L1.c.g2_ccs L1.c.g2_dhosp"
global v2_g2 "L2.c.g2 L2.c.g3 L2.c.g2_female L2.c.g2_partner L2.c.g2_empd L2.c.g2_incomel L2.c.g2_yed L2.c.g2_ced L2.c.g2_psych L2.c.g2_ccs L2.c.g2_dhosp"
global v1_h "L1.c.h_incomeg L1.c.hhsize L1.c.hh_g2 L1.c.hh_u18 L1.c.h_welfare"
global v2_h "L2.c.h_incomeg L2.c.hhsize L2.c.hh_g2 L2.c.hh_u18 L2.c.h_welfare"

global t1_g1 "c.births_t_t1 c.g1_workexp_t1 c.g1_empd_t1 i.g1_empdi_t1 c.g1_incomel_t1 c.g1_yed_t1 c.g1_psych_t1 c.g1_ccs_t1 c.g1_dhosp_t1 i.g1_mstatr_t1 i.p_empdi i.p_incomeli i.p_psych_t1 i.p_ccs_t1 i.p_dhosp_t1"
global t1_g21 "c.g2_1_yed_t1 c.g2_1_psych_t1 c.g2_1_ccs_t1 c.g2_1_dhosp_t1"
global t1_g2 "c.g2_t1 c.g2_female_t1"
global t1_h "c.h_incomeg_t1 c.hhsize_t1 c.hh_g2_t1 c.hh_u18_t1 c.h_welfare_t1"

global yd "g1_empd g1_empdi g1_incomel d tb dc births_t ager1 year"
global fd "female teenb_g1 ager1 year g"
global g1d "births_t g1_workexp g1_empd g1_empdi g1_incomel g1_yed g1_ced g1_psych g1_ccs g1_dhosp g1_mstatr_1 g1_mstatr_2 g1_mstatr_3 g1_mstatr_4 g1_partner p_empdi_1 p_empdi_2 p_empdi_3 p_incomeli p_psych p_ccs p_dhosp"
global g21d "g2_1_workexp g2_1_empd g2_1_incomel g2_1_yed g2_1_ced g2_1_partner g2_1_psych g2_1_ccs g2_1_dhosp"
global g2d "g2 g3 g2_female g2_empd g2_incomel g2_yed g2_ced g2_partner g2_psych g2_ccs g2_dhosp"
global hd "h_incomeg hhsize hh_g2 hh_u18 h_welfare"

cd $home


* Load and prep data
* ------------------------

use $data\data_230115.dta, clear

* sample setting
keep if t1<.
bysort pnrr: egen syear=min(year)
bysort pnrr: egen sage=min(ager1)
bysort pnrr: egen sinmin=min(g1_incomel)
bysort pnrr: egen sinmax=max(g1_incomel)
keep if sage>=20
keep if ager1<=64
drop if sinmin<0
drop if sinmax>=1000000
xtset pnrr t1

egen m1=rowmiss($d1 $g1 $g21 $g2 $h)
egen m2=rowmiss($y $d1 $f1)
sort pnrr year
bysort pnrr: gen s=1 if d_t1==0 & d_t11==0 & t1>=3 & t1<. & m1[_n-2]==0 & m1[_n-1]==0 & m2==0 & g1_die!=1 & emmi!=1 & g2_1_die!=1 & d1_d!=1
bysort pnrr: replace s=s*s[_n-1] if t1>=4
bysort pnrr: gen attr=0 if s==1
bysort pnrr: replace attr=1 if s[_n-1]==1 & s==.
bysort pnrr: replace attr=1 if t1==3 & m1[_n-2]==0 & s==.
bysort pnrr: replace s=0 if s[_n+1]==1 & t1==2
bysort pnrr: replace s=0 if s[_n+2]==1 & t1==1

gen tb=t1+12-$dc1
recode tb (min/19=1)(20/max=0)
replace tb=0 if $d1==0

* dummy variables
tab year5, gen(year5_)
tab g1_mstatr, gen(g1_mstatr_)
tab g2_1_mstatr, gen(g2_1_mstatr_)
tab p_empdi, gen(p_empdi_)

keep $yd $fd $g1d $g21d $g2d $hd $y $d1 $dc1 pnrr t1 s female g attr g1_die emmi g2_1_die d1_die b1_g2 *_imp

* for descriptive statistics
gen miss=1 if attr==1
replace miss=0 if miss==1 & (g1_die==1|emmi==1|g2_1_die==1|d1_die==1)

foreach i in d s b1_g2 tb attr miss g1_die emmi g2_1_die d1_die{
	bysort pnrr: egen e_`i'=max(`i')
}

gen before_d=1 if d==0 & d[_n+1]==1
gen before_tb=1 if tb==0 & tb[_n+1]==1

recode d (0=1)(1=3), gen(dtb)
replace dtb=2 if tb==1

********************************************************************************
********************************************** descriptive statistics by G1-year
********************************************************************************

log using $result\des_g1y_230115, smcl replace

* table dimension
local row=0
foreach i in $yd $fd $g1d $g21d $g2d $hd{
	local row=`row'+1
}

* Descriptive statistics at age 13/25 (Table A2)
* ------------------------------------------------

foreach t in 1{
	mat t`t'=J(`row',14,.)
	mat rownames t`t'=$yd $fd $g1d $g21d $g2d $hd
	local r=0
	foreach v in $yd $fd $g1d $g21d $g2d $hd{
		local r=`r'+1
		local c=0
		sum `v' if t1==`t' & s==0
		local c=`c'+1
		mat t`t'[`r',`c']=r(N)
		local c=`c'+1
		mat t`t'[`r',`c']=r(mean)
		forvalues i=0/1{
			sum `v' if female==`i' & t1==`t' & s==0
			local c=`c'+1
			mat t`t'[`r',`c']=r(N)
			local c=`c'+1
			mat t`t'[`r',`c']=r(mean)
			forvalues j=0/1{
				sum `v' if female==`i' & g==`j' & t1==`t' & s==0
				local c=`c'+1
				mat t`t'[`r',`c']=r(N)
				local c=`c'+1
				mat t`t'[`r',`c']=r(mean)
			}
		}		
	}
}
foreach t in 13{
	mat t`t'=J(`row',14,.)
	mat rownames t`t'=$yd $fd $g1d $g21d $g2d $hd
	local r=0
	foreach v in $yd $fd $g1d $g21d $g2d $hd{
		local r=`r'+1
		local c=0
		sum `v' if t1==`t' & s[_n+1]==1
		local c=`c'+1
		mat t`t'[`r',`c']=r(N)
		local c=`c'+1
		mat t`t'[`r',`c']=r(mean)
		forvalues i=0/1{
			sum `v' if female==`i' & t1==`t' & s[_n+1]==1
			local c=`c'+1
			mat t`t'[`r',`c']=r(N)
			local c=`c'+1
			mat t`t'[`r',`c']=r(mean)
			forvalues j=0/1{
				sum `v' if female==`i' & g==`j' & t1==`t' & s[_n+1]==1
				local c=`c'+1
				mat t`t'[`r',`c']=r(N)
				local c=`c'+1
				mat t`t'[`r',`c']=r(mean)
			}
		}		
	}
}

* Descriptive statistics one year before G2's childbearing (for Tables 1, A4, A5, and A6)
* -----------------------------------------------------------------------------------------

mat before_d=J(3,14,.)
mat rownames before_d=$y
local r=0
foreach v in $y{
	local r=`r'+1
	local c=0
	sum `v' if before_d==1 & s[_n+1]==1
	local c=`c'+1
	mat before_d[`r',`c']=r(N)
	local c=`c'+1
	mat before_d[`r',`c']=r(mean)
	forvalues i=0/1{
		sum `v' if female==`i' & before_d==1 & s[_n+1]==1
		local c=`c'+1
		mat before_d[`r',`c']=r(N)
		local c=`c'+1
		mat before_d[`r',`c']=r(mean)
		forvalues j=0/1{
			sum `v' if female==`i' & g==`j' & before_d==1 & s[_n+1]==1
			local c=`c'+1
			mat before_d[`r',`c']=r(N)
			local c=`c'+1
			mat before_d[`r',`c']=r(mean)
		}
	}
}

* Descriptive statistics one year before G2's teenage childbearing (for Tables 1, A4, A5, and A6)
* ------------------------------------------------------------------------------------------------

mat before_tb=J(3,14,.)
mat rownames before_tb=$y
local r=0
foreach v in $y{
	local r=`r'+1
	local c=0
	sum `v' if before_tb==1 & s[_n+1]==1
	local c=`c'+1
	mat before_tb[`r',`c']=r(N)
	local c=`c'+1
	mat before_tb[`r',`c']=r(mean)
	forvalues i=0/1{
		sum `v' if female==`i' & before_tb==1 & s[_n+1]==1
		local c=`c'+1
		mat before_tb[`r',`c']=r(N)
		local c=`c'+1
		mat before_tb[`r',`c']=r(mean)
		forvalues j=0/1{
			sum `v' if female==`i' & g==`j' & before_tb==1 & s[_n+1]==1
			local c=`c'+1
			mat before_tb[`r',`c']=r(N)
			local c=`c'+1
			mat before_tb[`r',`c']=r(mean)
		}
	}
}

* export matrices
foreach i in t1 t3 t13 mfb mfb0 mfb1 mfbt mfbt0 mfbt1 before_d before_tb miss{
	mat list `i'
}

log close

********************************************************************************
***************************************************** Inverse probability weight 
********************************************************************************

use $data\msm_230115.dta, clear

log using $result\des_weight_230115, smcl replace

* Descriptive statistics of weight (Table A3)
* ---------------------------------------------

tabstat wd wa wo, stat(n mean sd p1 p25 p50 p75 p99)
forvalues i=0/1{
	tabstat wd wa wo if female==`i', stat(n mean sd p1 p25 p50 p75 p99)
	forvalues j=0/1{
		tabstat wd wa wo if female==`i' & g==`j', stat(n mean sd p1 p25 p50 p75 p99)
	}
}

log close
